package com.zhongwang.mms.module.buy.listner;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.mms.common.ImportCallBack;
import com.zhongwang.mms.gen.entity.BReportPlanAddLog;
import com.zhongwang.mms.gen.entity.TDictionary;
import com.zhongwang.mms.gen.entity.TEmploy;
import com.zhongwang.mms.module.buy.dao.ReportPlanDao;
import com.zhongwang.mms.module.buy.model.ImportReportPlanModel;
import com.zhongwang.mms.module.buy.model.ReportPlanModel;
import com.zhongwang.mms.util.CodeUtil;
import com.zhongwang.mms.util.ShiroUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.zhongwang.mms.util.BeanUtil.copy;

/**
 * @Description: 补计划录入 listener
 * @Author: 徐一贺
 * @Date: 2020年9月3日16:06:22
 */
public class ImportReportPlanListener extends AnalysisEventListener<ImportReportPlanModel> {

    private List<TEmploy> employs;
    private ReportPlanDao reportPlanDao;
    private ImportCallBack callBack;
    private List<ImportReportPlanModel> reportPlanModels = new ArrayList<>();
    private List<TDictionary> categories;
    private List<Integer> exceptionList = new ArrayList<>();
    private int i = 2;

    public ImportReportPlanListener(ReportPlanDao reportPlanDao,
                                    ImportCallBack callBack,
                                    List<TDictionary> categories,
                                    List<TEmploy> employs) {
        this.reportPlanDao = reportPlanDao;
        this.callBack = callBack;
        this.categories = categories;
        this.employs = employs;
    }

    @Override
    public void invoke(ImportReportPlanModel data, AnalysisContext context) {
        if (data.getRppNum() != null) {
            BigDecimal bd = new BigDecimal(data.getRppNum());
            data.setRppNum(bd.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
        }
        TDictionary type = getType(data.getRppTypes());

        data.setReason("");

//        if (data.getRppProjectNum() == null) {
//            data.setReason(data.getReason() + "项目数量没填");
//        }
        if (data.getRppTypes() == null || data.getRppTypes().trim().equals("")) {
            data.setReason(data.getReason() + "采购类型没填");
        }
//        if (data.getRppPart() == null || data.getRppPart().trim().equals("")) {
//            data.setReason(data.getReason() + "部分没填");
//        }
//        if (data.getRppMalType() == null || data.getRppMalType().trim().equals("")) {
//            data.setReason(data.getReason() + "物料类型没填");
//        }
        if (data.getRppMatDesc() == null) {
            data.setReason(data.getReason() + "物料描述没填");
        }
        if (data.getRppNum() == null) {
            data.setReason(data.getReason() + "数量没填");
        } else if (data.getRppNum() <= 0) {
            data.setReason(data.getReason() + "数量必须大于0");
        }

        if (data.getRppUnit() == null || data.getRppUnit().trim().equals("")) {
            data.setReason(data.getReason() + "单位没填");
        }

        if (data.getRppProjectCode() == null || data.getRppProjectCode().trim().equals("")) {
            data.setReason(data.getReason() + "项目编号没填");
        } else if (reportPlanDao.findProjectByCode(data.getRppProjectCode()) == null) {
            data.setReason(data.getReason() + "项目编号不存在");
        }

        if (data.getRppProjectName() == null || data.getRppProjectName().trim().equals("")) {
            data.setReason(data.getReason() + "项目名称没填");
        }

        if (data.getRppPlanNo() == null || data.getRppPlanNo().trim().equals("")) {
            data.setReason(data.getReason() + "计划号没填");
        }

//        if (data.getRppUseStoreStr() == null || data.getRppUseStoreStr().trim().equals("")) {
//            data.setReason(data.getReason() + "是否使用库存没填");
//        } else
        if (data.getRppUseStoreStr() != null && !"是".equals(data.getRppUseStoreStr()) && !"否".equals(data.getRppUseStoreStr())) {
            data.setReason(data.getReason() + "是否使用库存是能填是或者否");
        }

        if (type == null) {
            data.setReason(data.getReason() + "采购类别不存在");
        } else if (type.getDicName().equals("外协件")) {
            if (data.getRppDiagram() == null || data.getRppDiagram().trim().equals("")) {
                data.setReason(data.getReason() + "图号没填");
            }
            if (data.getRppAssistType() == null || data.getRppAssistType().trim().equals("")) {
                data.setReason(data.getReason() + "外协类型没填");
            } else if (reportPlanDao.findRppAssistType(data.getRppAssistType()) == null) {
                data.setReason(data.getReason() + "外协类型不存在");
            }
        }

//        if (data.getRppDesigner() == null || data.getRppDesigner().trim().equals("")) {
//            data.setReason(data.getReason() + "提报人没填");
//        }
//
//
//
//
//        if (data.getRppDesignerTel() == null || data.getRppDesignerTel().trim().equals("")) {
//            data.setReason(data.getReason() + "提报人联系方式没填");
//        }

        if (data.getRppPlanner() == null || data.getRppPlanner().trim().equals("")) {
            data.setReason(data.getReason() + "计划员没填");
        } else if (getPlanner(data.getRppPlanner()) == null) {
            data.setReason(data.getReason() + "计划员不存在");
        }

        if (data.getRppSupReasons() == null || data.getRppSupReasons().trim().equals("")) {
            data.setReason(data.getReason() + "补录原因没填");
        }

        if (reportPlanDao.findProjectByCode(data.getRppProjectCode()) == null) {
            data.setReason(data.getReason() + "项目号不存在");
        }
        // 项目号转换八位
        data.setRppProjectCode(CodeUtil.projectCodeHandle(data.getRppProjectCode()));
        data.setRppCreateTime(new Date());
        data.setRppOriginalFlag(1);
        data.setRppStatus(-1);
        data.setRppAllowCancel(2);
        data.setRppUseStock(0);
        data.setRppTypeId(type.getDicId());
        data.setRppCreateUser(ShiroUtils.getUser().getName());
        data.setRppIsFlag(0);
        ReportPlanModel reportPlanModel = new ReportPlanModel();
        copy(data, reportPlanModel);
        if (reportPlanDao.findExist(reportPlanModel) > 0) {
            data.setReason(data.getReason() + "重复数据");
        }
        // 验证
        if (data.getReason().length() > 0) {
            reportPlanModels.add(data);
            i++;
            return;
        }

        ReportPlanModel model = new ReportPlanModel();
        copy(data, model);
        if(data.getRppUseStoreStr()!=null){
            if("是".equals(data.getRppUseStoreStr())){
                model.setRppUseStore(1);
            }else if("否".equals(data.getRppUseStoreStr())){
                model.setRppUseStore(0);
            }
        }
        model.setRppSplitPlanType("source");
        //如果是原材料是否使用库存必填，外协类型和图号是null
        if("原材料".equals(model.getRppTypes())){
            model.setRppAssistType(null);
            model.setRppDiagram(null);
        }
        //如果是外协件图号和外协类型必填，是否使用库存是null
        if("外协件".equals(model.getRppTypes())){
            model.setRppUseStore(null);
        }
        //如果是外购件使用库存是null，外协类型和图号是null
        if("外购件".equals(model.getRppTypes())){
            model.setRppAssistType(null);
            model.setRppDiagram(null);
            model.setRppUseStore(null);
        }
        reportPlanDao.insert(model);
        BReportPlanAddLog bReportPlanAddLog = new BReportPlanAddLog();
        bReportPlanAddLog.setCalRppId(model.getRppId());
        bReportPlanAddLog.setCalSupReasons(model.getRppSupReasons());
        bReportPlanAddLog.setCalDate(new Date());
        bReportPlanAddLog.setCalUid(ShiroUtils.getUserId());
        reportPlanDao.insertBReportPlanAddLog(bReportPlanAddLog);
        i++;
    }

    private TDictionary getType(String rppTypes) {
        for (TDictionary category : categories) {
            if (category.getDicName() != null && category.getDicName().equals(rppTypes)) {
                return category;
            }
        }
        return null;
    }

    private TEmploy getPlanner(String planner) {
        for (TEmploy employ : employs) {
            if (employ.getEmpName() != null && employ.getEmpName().equals(planner)) {
                return employ;
            }
        }
        return null;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (exceptionList.size() > 0) {
            ImportReportPlanModel model = new ImportReportPlanModel();
            model.setRppMatDesc("第" + exceptionList.toString() + "行数据格式不正确");
            reportPlanModels.add(model);
        }
        if (reportPlanModels.size() > 0) {
            callBack.onWarning(reportPlanModels);
        } else {
            callBack.onSuccess();
        }
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) {
        exception.printStackTrace();
        String err = exception.getMessage();
        exceptionList.add(i);
        i++;
    }

}